# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=vim
pkgver=9.0.0009
pkgrel=0
pkgdesc="Improved vi-style text editor"
url="https://www.vim.org/"
arch="all"
license="Vim"
options="!check"  # some tests are failing
depends="xxd"
makedepends="
	gtk+3.0-dev
	libx11-dev
	libxt-dev
	lua5.4-dev
	ncurses-dev
	python3-dev
	ruby-dev
	"
subpackages="$pkgname-doc ${pkgname}diff::noarch $pkgname-tutor gvim xxd"
source="$pkgname-$pkgver.tar.gz::https://github.com/vim/vim/archive/v$pkgver.tar.gz
	vimrc
	"

# secfixes:
#   8.2.5170-r0:
#     - CVE-2022-2124
#     - CVE-2022-2125
#     - CVE-2022-2126
#     - CVE-2022-2129
#   8.2.5055-r0:
#     - CVE-2022-1851
#     - CVE-2022-1886
#     - CVE-2022-1898
#   8.2.5000-r0:
#     - CVE-2022-1796
#   8.2.4969-r0:
#     - CVE-2022-1619
#     - CVE-2022-1620
#     - CVE-2022-1621
#     - CVE-2022-1629
#   8.2.4836-r0:
#     - CVE-2022-1381
#   8.2.4708-r0:
#     - CVE-2022-1154
#     - CVE-2022-1160
#   8.2.4619-r0:
#     - CVE-2022-0943
#   8.2.4542-r0:
#     - CVE-2022-0572
#     - CVE-2022-0629
#     - CVE-2022-0685
#     - CVE-2022-0696
#     - CVE-2022-0714
#     - CVE-2022-0729
#   8.2.4350-r0:
#     - CVE-2022-0359
#     - CVE-2022-0361
#     - CVE-2022-0368
#     - CVE-2022-0392
#     - CVE-2022-0393
#     - CVE-2022-0407
#     - CVE-2022-0408
#     - CVE-2022-0413
#     - CVE-2022-0417
#     - CVE-2022-0443
#   8.2.4173-r0:
#     - CVE-2021-4069
#     - CVE-2021-4136
#     - CVE-2021-4166
#     - CVE-2021-4173
#     - CVE-2021-4187
#     - CVE-2021-4192
#     - CVE-2021-4193
#     - CVE-2021-46059
#     - CVE-2022-0128
#     - CVE-2022-0156
#     - CVE-2022-0158
#     - CVE-2022-0213
#   8.2.3779-r0:
#     - CVE-2021-4019
#   8.2.3650-r0:
#     - CVE-2021-3927
#     - CVE-2021-3928
#     - CVE-2021-3968
#     - CVE-2021-3973
#     - CVE-2021-3974
#     - CVE-2021-3984
#   8.2.3567-r0:
#     - CVE-2021-3903
#   8.2.3500-r0:
#     - CVE-2021-3875
#   8.2.3437-r0:
#     - CVE-2021-3770
#     - CVE-2021-3778
#     - CVE-2021-3796
#   8.1.1365-r0:
#     - CVE-2019-12735
#   8.0.1521-r0:
#     - CVE-2017-6350
#     - CVE-2017-6349
#   8.0.0329-r0:
#     - CVE-2017-5953
#   8.0.0056-r0:
#     - CVE-2016-1248

prepare() {
	# Read vimrc from /etc/vim
	echo '#define SYS_VIMRC_FILE "/etc/vim/vimrc"' >> src/feature.h
	cp -r "$builddir" "$builddir"-gvim
	default_prepare
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-luainterp=dynamic \
		--enable-python3interp=dynamic \
		--enable-rubyinterp=dynamic \
		--without-x \
		--disable-nls \
		--enable-acl \
		--enable-multibyte \
		--enable-gui=no \
		--with-lua-prefix=/usr/lua5.4 \
		--with-compiledby="Alpine Linux"
	make
	cd "$builddir"-gvim
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-luainterp=dynamic \
		--enable-python3interp=dynamic \
		--enable-rubyinterp=dynamic \
		--with-x \
		--disable-nls \
		--enable-acl \
		--enable-multibyte \
		--enable-gui=gtk3 \
		--with-lua-prefix=/usr/lua5.4 \
		--with-compiledby="Alpine Linux"
	make
}

check() {
	make test
}

package() {
	make -j1 DESTDIR="$pkgdir/" install

	install -Dm644 runtime/doc/uganda.txt \
		"$pkgdir/usr/share/licenses/$pkgname/LICENSE"
	install -Dm644 "$srcdir"/vimrc "$pkgdir"/etc/vim/vimrc
}

gvim() {
	pkgdesc="advanced text editor, with GUI"
	provides="vim"
	cd "$builddir"-gvim
	make -j1 DESTDIR="$subpkgdir/" install

	install -Dm644 runtime/doc/uganda.txt \
		"$subpkgdir/usr/share/licenses/$pkgname/LICENSE"
	install -Dm644 "$srcdir"/vimrc "$subpkgdir"/etc/vim/vimrc
	rm -rf "$subpkgdir"/usr/share/man
	rm "$subpkgdir/usr/bin/xxd"
}

vimdiff() {
	pkgdesc="view file diffs in vim"
	depends="diffutils vim"
	options="!tracedeps"

	install -d "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/vimdiff "$subpkgdir"/usr/bin
}

tutor() {
	pkgdesc="vim tutor and tutor files"
	depends="vim"
	options="!tracedeps"
	local _v=$(echo ${pkgver%.*} | tr -d '.')

	install -d "$subpkgdir"/usr/bin
	install -d "$subpkgdir"/usr/share/vim/vim$_v/tutor
	mv "$pkgdir"/usr/bin/vimtutor "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/share/vim/vim$_v/tutor "$subpkgdir"/usr/share/vim/vim$_v/
}

xxd() {
	pkgdesc="tool to make (or reverse) a hex dump"
	depends=""
	echo "$pkgdir" "$subpkgdir"
	mkdir -p "$subpkgdir/usr/bin"
	mv "$pkgdir/usr/bin/xxd" \
	   "$subpkgdir/usr/bin/"
}

sha512sums="
d6f725b29853a54282c12d161b145fc45064cdfba5721a57e7947df5aa846a8f9260c823fc8b8d2a879fc3c38b832b9ab7a2a4e3b3f941d78cdad97750eb3cae  vim-9.0.0009.tar.gz
9ed58e38fe9d72f934eb507b27c18590c33c648eb7ca2639a9afdcaa1c2c4e0873a4e4252d2b6cc28127d1ded227a1e8ac338c93958526f98a3b66dbdf6afcc8  vimrc
"
